package com.shine.customview.widget.viewgroup;

import android.content.Context;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

/**
 * Created by M_genius on 2017/9/1 11:06.
 * 描述：ViewDragHelper 的使用
 *      此容器分为两种情况
 *          1、其子view不消费事件
 *              此时，直接进入onTouchEvent，执行viewDragHelper.processTouchEvent(event);在这里，会直接去捕获子view，回调捕获成功
 *          2、子view消费事件
 *              此时、则进入shouldInterceptTouchEvent,MOVE的时候经过一系列的判断（getViewHorizontalDragRange，clampViewPositionVertical等），才能够去tryCaptureView
 *
 */

public class ViewDragHelperLayout extends LinearLayout {

    ViewDragHelper viewDragHelper;
    private View mDragView;
    private View mAutoBackView;
    private View mEdgeTrackerView;
    private Point mAutoBackOriginPos = new Point();


    public ViewDragHelperLayout(Context context) {
        this(context, null);
    }

    public ViewDragHelperLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ViewDragHelperLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        viewDragHelper = ViewDragHelper.create(this, (float) 1.0, new ViewDragHelper.Callback() {
            @Override
            public boolean tryCaptureView(View child, int pointerId) {
                //mEdgeTrackerView禁止直接移动
                return child == mDragView || child == mAutoBackView;
            }

            // 在该方法中对child移动的边界进行控制
            @Override
            public int clampViewPositionHorizontal(View child, int left, int dx) {
//                int leftBound = getPaddingLeft();
//                int rightBound = getWidth() - child.getMeasuredWidth() - getPaddingRight();
//                int newLeft = Math.min(Math.max(left, leftBound), rightBound);
                return left;
            }

            // 在该方法中对child移动的边界进行控制
            @Override
            public int clampViewPositionVertical(View child, int top, int dy) {
//                int topBound = getPaddingTop();
//                int bottomBound = getHeight() - child.getMeasuredHeight() - getPaddingBottom();
//                int newTop = Math.min(Math.max(top,topBound),bottomBound);
                return top;
            }

            //手指释放的时候回调
            @Override
            public void onViewReleased(View releasedChild, float xvel, float yvel) {
                //mAutoBackView手指释放时可以自动回去
                if (releasedChild == mAutoBackView) {
                    viewDragHelper.settleCapturedViewAt(mAutoBackOriginPos.x, mAutoBackOriginPos.y);
                    invalidate();
                }
            }

            //在边界拖动时回调
            @Override
            public void onEdgeDragStarted(int edgeFlags, int pointerId) {
                viewDragHelper.captureChildView(mEdgeTrackerView, pointerId);
            }
        });
        viewDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
    }

    @Override
    protected void onFinishInflate()
    {
        super.onFinishInflate();

        mDragView = getChildAt(0);
        mAutoBackView = getChildAt(1);
        mEdgeTrackerView = getChildAt(2);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        super.onLayout(changed, l, t, r, b);

        mAutoBackOriginPos.x = mAutoBackView.getLeft();
        mAutoBackOriginPos.y = mAutoBackView.getTop();
    }


    @Override
    public void computeScroll() {
        if (viewDragHelper.continueSettling(true)) {
            invalidate();
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return viewDragHelper.shouldInterceptTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        viewDragHelper.processTouchEvent(event);
        return true;
    }

    public int getViewHorizontalDragRange(View child)
    {
        return getMeasuredWidth()-child.getMeasuredWidth();
    }

    public int getViewVerticalDragRange(View child)
    {
        return getMeasuredHeight()-child.getMeasuredHeight();
    }


}
